<template>
  <div class="container">
    <el-button
      size="small"
      type="primary"
      @click="showAdd"
      style="margin-bottom: 20px; float: right"
      >添加班级 <i class="el-icon-circle-plus-outline"></i
    ></el-button>
    <el-table
      :row-class-name="xxxTableRowClassName"
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{
        background: 'rgba(61,157,255,0.05)',
        color: '#333333',
      }"
    >
      <el-table-column
        prop="scName"
        label="年级名称"
        width="width"
        align="center"
        sortable
      >
      </el-table-column>
      <el-table-column
        prop="pscName"
        label="班级名称"
        width="width"
        align="center"
        sortable
      >
      </el-table-column>
      <el-table-column
        prop="realName"
        label="任课老师"
        width="width"
        align="center"
        sortable
      >
      </el-table-column>
      <el-table-column
        prop="headTeacher"
        label="任课班主任"
        width="width"
        align="center"
        sortable
      >
      </el-table-column>
      <el-table-column label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            size="small"
            plain
            @click="compile($event, row)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" plain @click="del($event, row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :close-on-click-modal="false"
      :title="form.pscId ? '编辑班级' : '添加班级'"
      :visible.sync="dialogFormVisible"
      width="1000px"
    >
      <el-form
        label-position="left"
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="160px"
      >
        <el-form-item label="年级:" style="width: 80%" prop="scId">
          <el-select
            v-model="form.scId"
            placeholder="请选择年级"
            style="width: 100%"
          >
            <el-option
              v-for="item in gradeList"
              :key="item.scId"
              :label="item.scName"
              :value="item.scId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级:" style="width: 80%" prop="pscName">
          <el-input
            v-model="form.pscName"
            style="width: 100%"
            placeholder="请输入班级名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="任课老师:" style="width: 80%" prop="userId">
          <el-select
            v-model="form.userId"
            placeholder="请选择任课老师"
            style="width: 100%"
          >
            <el-option
              v-for="item in teacherList"
              :key="item.teacherId"
              :label="item.teacherName"
              :value="item.teacherId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任课班主任:" style="width: 80%" prop="headTeacherId">
          <el-select
            v-model="form.headTeacherId"
            placeholder="请选择任课班主任"
            style="width: 100%"
          >
            <el-option
              v-for="item in teacherLists"
              :key="item.teacherId"
              :label="item.teacherName"
              :value="item.teacherId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="课时:"> </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button
          size="small"
          :loading="disable"
          type="primary"
          @click="save"
          >{{ disable ? "加载中" : "确 定" }}</el-button
        >
      </div>
    </el-dialog>
    <pagination-page @size="changeSize" @page="changePage" :total="total" />
  </div>
</template>

<script>
import {
  classList,
  addClass,
  editClass,
  deleteClass,
} from "@/api/school/class/index.js";
import { gradeList } from "@/api/conmmon/index.js";
import { teacherList } from "@/api/school/teacher/index.js";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      disable: false,
      dialogFormVisible: false,
      total: -1,
      page: {
        pageSize: 10,
        pageNum: 1,
      },
      rules: {
        scId: [
          {
            required: true,
            message: "请选择年级",
          },
        ],
        pscName: [
          {
            required: true,
            message: "请输入班级名称",
          },
        ],
        userId: [
          {
            required: true,
            message: "请指定任教老师",
          },
        ],
        headTeacherId: [
          {
            required: true,
            message: "请指定任教老师",
          },
        ],
      },
      tableData: [],
      form: {
        pscName: "",
        scId: "",
        userId: "",
        headTeacherId: "",
        pscId:''
      },
      gradeList: [],
      teacherList: [],
      teacherLists: [],
      isLoading: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getgradeList();
    this.getclassList();
    this.getTeacherList();
  },
  methods: {
    //年级下拉列表
    getgradeList() {
      gradeList({ schoolId: this.$store.state.user.USER_INFO.pscShId }).then(
        (res) => {
          if (res.code == 200) {
            this.gradeList = res.result;
          }
        }
      );
    },
    xxxTableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 !== 0) {
        return "statistics-warning-row";
      } else {
        return "";
      }
    },
    //教师下拉列表
    getTeacherList() {
      let data = {
        pageNum: 1,
        pageSize: 99999,
        schoolId: this.$store.state.user.USER_INFO.pscShId,
      };
      teacherList(data).then((res) => {
        if (res.code == 200) {
          this.teacherList = res.result
            .map((item) => {
              if (item.teacherType == "2") {
                return item;
              }
            })
            .filter((item2) => item2 !== undefined);
          this.teacherLists = res.result
            .map((item) => {
              if (item.teacherType == "5") {
                return item;
              }
            })
            .filter((item2) => item2 !== undefined);
        }
      });
    },
    //获取班级列表
    getclassList() {
      let data = {
        pscShId: this.$store.state.user.USER_INFO.pscShId,
        ...this.page,
      };
      this.isLoading = true;
      classList(data).then((res) => {
        if (res.code == 200) {
          this.tableData = res.result.list;
          this.total = res.result.total;
          this.isLoading = false;
        }
      });
    },
    //编辑按钮
    compile(evt, row) {
      let target = evt.target;
      if (target.nodeName == "SPAN") {
        target = evt.target.parentNode;
      }
      target.blur();
      this.dialogFormVisible = true;
      this.form = {
        ...row,
      };
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate();
      });
    },
    //添加班级
    showAdd() {
      this.form = {
        pscName: "",
        scId: "",
        userId: "",
        headTeacherId: "",
      };

      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate();
      });
    },

    //确定保存
    save() {
      // 对所有表单项进行统一校验
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          this.disable = true;
          let data = {
            pscShId: this.$store.state.user.USER_INFO.pscShId,
            headTeacherId: this.form.headTeacherId,
            pscName: this.form.pscName,
            scId: this.form.scId,
            userId: this.form.userId,
            pscId:this.form.pscId
          };
          if (this.form.pscId) {
            editClass(data).then((res) => {
              if (res.code == 200) {
                this.$message.success("保存成功!");
                this.getclassList(this.page.pageSize);
                this.dialogFormVisible = false;
                this.disable = false;
              } else {
                this.$message.error(res.message);
                this.disable = false;
              }
            });
          } else {
            addClass(data).then((res) => {
              if (res.code == 200) {
                this.disable = false;
                this.dialogFormVisible = false;
                this.$message.success("保存成功!");
                this.getclassList();
              } else {
                this.disable = false;
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    //删除
    del(evt, row) {
      let target = evt.target;
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();
          let data = {
            pscId: row.pscId,
          };

          deleteClass(data).then((res) => {
            if (res.code == 200) {
              this.getclassList(
                this.tableData.length === 1 && this.page.pageSize > 1
                  ? this.page.pageSize - 1
                  : this.page.pageSize
              );
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();
          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },
    changeSize(size) {
      this.page.pageSize = size;
      this.getclassList();
    },
    changePage(page) {
      this.page.pageNum = page;
      this.getclassList();
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-table__row.statistics-warning-row {
  background: rgba(61, 157, 255, 0.05);
}
::v-deep .el-form-item__label {
  padding-left: 60px;
}
</style>
